<?php

namespace app\api\controller\v1;

use addons\adszone\library\Adszone;
use app\admin\controller\Zlog;
use app\common\controller\Api;
use app\common\logic\CourseLogic;
use app\common\logic\LearnLogic;
use app\common\model\course\Chapter;
use app\common\model\course\Course;
use app\common\model\course\Order;
use think\Db;

/**
 * 会员接口
 */
class Learn extends Api
{
    protected $noNeedLogin = ['getCourse','getSerieslist','getBanner','learnCateCourse1','learnCateSpecial','learnCateSpecialCourse','getTeachers'];  //不能放*
    protected $noNeedRight = '*';

    /*
     * 家长课程查询
     *
     */
    public function learnCateCourse1(){
        $type = $this->request->post("type");
        $learn_cate_id = $this->request->post("learn_cate_id");
        $learnCateCourse1List = (new LearnLogic())->getLearnCateCourse1($learn_cate_id, $type);
        $this->success('家长课程', $learnCateCourse1List);
    }

    public function getBanner() {
        $learn_cate_id = $this->request->post("learn_cate_id/d", 0);//课程类别
        $tag = 'parent_img';
        switch ($learn_cate_id) {
            case 1: $tag = 'parent_img';break;
            case 2: $tag = 'teacher_banner';break;
            case 3: $tag = 'students_img';break;
            case 4: $tag = 'must_img';break;
        }
        $pic = new Adszone();
        $list = $pic->getAdsByMark($tag,ORG_CATE_ID);
        $this->success('轮播图', $list);
    }

    /*
     * 大咖课程查询
     *
     */
    public function learnCateCourse2(){
        $learn_cate_teache_id = $this->request->post("learn_cate_teache_id");
        $learnCateCourse1List = (new LearnLogic())->getLearnCateCourse2($learn_cate_teache_id);
        $this->success('教职课程', $learnCateCourse1List);
    }

    /*
     * 专题分类列表
     */
    public function  learnCateSpecial(){
        $learnCateSpecialList = (new LearnLogic())->getLearnCateSpecial();
        $all = [
           'id' => 0,
           'learn_cate_id' => 5,
            'name' => '全部专题',
        ];
        array_unshift($learnCateSpecialList, $all);
        $this->success('专题分类列表', $learnCateSpecialList);
    } 
    
    /*专题分类课程列表*/
    public function learnCateSpecialCourse(){
        $learnCateSpecialId = $this->request->post('special_id');
        $specialList = (new LearnLogic())->getLearnCateSpecialCourse($learnCateSpecialId);
        $this->success('专题课程', $specialList);
    }

    //获取系列课课程详情
    public function getSerieslist() {
        $course_id = input('course_id/d', 0);
        if (!intval($course_id)) {
            $this->error('课程ID未传递');
        }
        $list = (new CourseLogic())->getHistory($course_id);
        $info = (new CourseLogic())->getSeries($course_id);
        $data = [
            'info'=>$info,
            'list'=>$list
        ];
        $this->success('课程详情', $data);
    }
    public function getCourse() {
        $course_id = input('course_id/d', 0);
        if (!intval($course_id)) {
            $this->error('课程ID未传递');
        }

        $info = (new CourseLogic())->getCourse($course_id, true);
        $this->success('课程详情', $info);
    }

    public function thumbsDo()
    {
        $course_id = input('course_id/d', 0);
        if (!intval($course_id)) {
            $this->error('课程ID未传递');
        }
        $type = input('type/d', 0);//1点赞 2取消点赞
        if (!in_array($type, array(1,2))) {
            $this->error('类型未传递');
        }
        $res = (new CourseLogic())->doThumbs($course_id, $type);
        if($res){
            $this->success('操作成功！');
        }else{
            $this->error('操作失败！');
        }
    }

    //大咖老师列表
    public function getTeachers() {
        $learnLogic = new LearnLogic();
        $learnCateSpecialList = $learnLogic->getTeachers();
        //对所有老师的简介 进行处理

        //获取推荐的老师 同时只能设置一个推荐老师
        $tuijian = $learnLogic->getTeachers(['is_tuijian'=> 1], 1);
        $tuijian = $tuijian ? $tuijian[0] : [];
//        $all = [
//            'id' => 0,
//            'learn_cate_id' => 5,
//            'name' => '全部名师',
//        ];
//        array_unshift($learnCateSpecialList, $all);
        $pic = new Adszone();
        $images = $pic->getAdsByMark('teacher_img',ORG_CATE_ID);
        $image = isset($images['data'][0]['imageurl'])?$images['data'][0]['imageurl']:'';
        $data = [
            'tuijian' => $tuijian,
            'list'    => $learnCateSpecialList,
            'image'=> $image
        ];
        $this->success('专题分类列表', $data);
    }

    //大咖老师课程
    public function getTeachersCourse($teacher_id) {
        $teacher_id = input('teacher_id/d', 0);
        $list = (new LearnLogic())->getTeachersCourse($teacher_id);
        $this->success('课程详情', $list);
    }


    //获取老师详情
    public function getTeacherInfo() {
        $teacher_id = input('teacher_id/d', 0);
        if (!intval($teacher_id)) {
            $this->error('老师ID未传递');
        }

        $info = Db::name('learn_cate_teacher')->find($teacher_id);
        //获取前4个点赞做多的课程
        $list = (new LearnLogic())->getTeachersCourse($teacher_id, 5, 'thumbs desc');
        $list = $list->toArray();
        $this->success('老师详情', [
            'info' => $info,
            'list' => $list['data']
        ]);
    }

    /*
     *  章节列表
     * */
    public function getChapter() {
        $id = input('id/d', 0);
        //不分页 一次性获取所有章节
        $list = (new CourseLogic())->getChapters($id);

        $this->success('章节列表', $list);
    }

    /*
     * 获取章节详情
     * */
    public function getChapterDetail() {
        $id = input('id/d', 0);
        if (!$id) {
            $this->error('章节id必须传递');
        }

        $info = Chapter::get($id);
        if (!$info) {
            $this->error('获取课程信息错误');
        }
        //查看课程是否购买
        $where['user_id'] = UID;
        $where['learn_course_id'] = $info['edu_learn_course_id'];
        $where['type'] = 1;
        $res = (new Order())->where($where)->count();
        $course = Course::get($info['edu_learn_course_id']);
        if ($info['is_audition'] == 1 && $course['price'] != 0 && $res < 1) {
            $this->error('请先购买课程');
        }

        $this->success('章节详情', $info);
    }


    /*
     * 获取热门课程 热门课程 这里是用购买数量来确定的
     * */
    public function getHot() {
        $list = (new LearnLogic())->getHotCourse(15);
        $this->success('list', $list);
    }

}
